part of 'index.dart';

//class PlatformNotificationPage extends StatefulWidget {

class PlatformNotificationPage extends StatefulWidget {
  const PlatformNotificationPage(this.conversation, {super.key});
  final EMConversation conversation;

  @override
  State<PlatformNotificationPage> createState() =>
      _PlatformNotificationPageState();
}

class _PlatformNotificationPageState extends State<PlatformNotificationPage> {
  final refreshController = EasyRefreshController(
    controlFinishRefresh: true,
    controlFinishLoad: true,
  );
  List<EMMessage> messageList = [];

  final int _limit = 20;
  bool _noMore = false;
  String? startMsgId;

  @override
  void initState() {
    super.initState();
    _getMessageList();
  }

  Future<void> _getMessageList({String? start}) async {
    final conversationId = widget.conversation.id;
    final result = await ImManager.of.loadLocalHistoryMessage(
      conversationId,
      startMsgId: start ?? startMsgId,
      limit: _limit,
    );
    messageList.addAll(result);
    messageList.sort((a, b) => b.serverTime.compareTo(a.serverTime));
    if (messageList.isNotEmpty) startMsgId = messageList.first.msgId;
    if (result.length < _limit) _noMore = true;
    setState(() {});
  }

  void onLoading() async {
    if (_noMore) {
      refreshController.finishLoad(IndicatorResult.noMore);
      return;
    }
    try {
      await _getMessageList();
      refreshController.finishLoad();
    } catch (error) {
      refreshController.finishLoad(IndicatorResult.fail);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: const Color(0xFFDCD4FF),
        title: Text(
          T.current.c_plt_notice,
          style: const TextStyle(fontSize: 22, fontWeight: FontWeight.w500),
        ),
      ),
      backgroundColor: const Color(0xFFFAFAFA),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 15),
        child: CustomPullScrollView(
          controller: refreshController,
          onLoading: onLoading,
          refreshOnStart: false,
          builder: (context, physics) => CustomScrollView(
            physics: physics,
            slivers: [
              if (messageList.isEmpty)
                Padding(
                  padding: const EdgeInsets.only(top: 100),
                  child: Image.asset(
                    Images.bgPlaceEmpty,
                    width: 200,
                    height: 200,
                  ),
                ).sliver,
              SliverList.builder(
                itemBuilder: (context, index) {
                  return _Card(messageList[index]);
                },
                itemCount: messageList.length,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class _Card extends StatelessWidget {
  const _Card(this.model);
  final EMMessage model;

  @override
  Widget build(BuildContext context) {
    final attributes = model.attributes ?? {};
    // ignore: unused_local_variable
    final title = attributes['title'] as String?;
    final content = attributes['content'] as String?;
    return Padding(
      padding: EdgeInsets.only(
        right: Tools.isAr ? 0 : 20,
        left: Tools.isAr ? 20 : 0,
        top: 10,
      ),
      child: Column(
        children: [
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 2),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(99),
              color: const Color(0xFF4A4A4A).withOpacity(0.1),
            ),
            child: Text(
              Tools.dateFromMS(model.serverTime),
              style: const TextStyle(color: Colors.white, fontSize: 12),
            ),
          ),
          Row(
            children: [
              Transform.flip(
                flipX: Tools.isAr,
                child: Image.asset(Images.icChatVolume, width: 42, height: 42),
              ),
              Container(
                margin: EdgeInsets.only(
                  left: Tools.isAr ? 0 : 10,
                  right: Tools.isAr ? 10 : 0,
                  top: 10,
                  bottom: 12,
                ),
                padding: const EdgeInsets.symmetric(
                  horizontal: 14,
                  vertical: 14,
                ),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8),
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(
                      color: const Color(0xFF787878).withOpacity(0.1),
                      spreadRadius: 1,
                      blurRadius: 5,
                      offset: const Offset(4, 0),
                    )
                  ],
                ),
                child: Text(content ?? ''),
              ).expanded,
            ],
          ),
        ],
      ),
    );
  }
}
